package fr.ordre66.vlcs.utils;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;



public class GlobalHttpClient extends DefaultHttpClient {


	public GlobalHttpClient() {
		super();
		addRequestInterceptor();
		addResponseInterceptor();
	}

	public GlobalHttpClient(boolean https) {
		super();

//		if (https) {
//			try {
//				KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
//				FileInputStream instream = new FileInputStream(System.getenv("HOME") + "/.keystore");
//
//				try {
//					trustStore.load(instream, "anr_2008".toCharArray());
//				} finally {
//					instream.close();
//				}
//
//				SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
//				// socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
//				Scheme sch = new Scheme("https", socketFactory, 8443);
//				this.getConnectionManager().getSchemeRegistry().register(sch);
//			} catch (KeyManagementException e) {
//				System.err.println(e.getMessage());
//			} catch (UnrecoverableKeyException e) {
//				System.err.println(e.getMessage());
//			} catch (KeyStoreException e) {
//				System.err.println(e.getMessage());
//			} catch (FileNotFoundException e) {
//				System.err.println(e.getMessage());
//			} catch (NoSuchAlgorithmException e) {
//				System.err.println(e.getMessage());
//			} catch (CertificateException e) {
//				System.err.println(e.getMessage());
//			} catch (IOException e) {
//				System.err.println(e.getMessage());
//			}
//		}

		addRequestInterceptor();
		addResponseInterceptor();
	}

	public GlobalHttpClient(ClientConnectionManager conman, HttpParams params) {
		super(conman, params);
		addRequestInterceptor();
		addResponseInterceptor();
	}

	public GlobalHttpClient(HttpParams params) {
		super(params);
		addRequestInterceptor();
		addResponseInterceptor();
	}

	private void addRequestInterceptor() {

		this.addRequestInterceptor(new HttpRequestInterceptor() {

			public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
				if (!request.containsHeader("Accept-Encoding")) {
					System.out.println("add header Accept-Encoding with gzip value");
					request.addHeader("Accept-Encoding", "gzip");
				}
			}

		});
	}

	private void addResponseInterceptor() {
		this.addResponseInterceptor(new HttpResponseInterceptor() {

			public void process(HttpResponse response,
					org.apache.http.protocol.HttpContext context)
					throws HttpException, IOException {
				HttpEntity entity = response.getEntity();
				if (entity == null)
					return;
				Header ceheader = entity.getContentEncoding();
				if (ceheader != null) {
					HeaderElement[] codecs = ceheader.getElements();
					for (int i = 0; i < codecs.length; i++) {
						if (codecs[i].getName().equalsIgnoreCase("gzip")) {
							response.setEntity(new GzipDecompressingEntity(response.getEntity()));
							System.out.println("Gzip decompression");
							return;
						}
					}
				}
			}

		});
	}

}
